<?php

namespace app\merch\controller;

use think\Controller;
use think\Db;
use think\Exception;

class Login extends Controller {

    public function index() {
        $_GPC = input();
        if ($this->request->isPost()) {
            try {
                if ($_GPC['mobile'] == '') {
                    jsont([], 1, '请填写手机号！');
                }
                if ($_GPC['pwd'] == '') {
                    jsont([], 1, '请填写密码！');
                }

                $data = pdo_get('member', array('ismerch' => 1, 'mobile' => $_GPC['mobile']));
                if (!$data) {
                        jsont([], 1, '手机号或密码错误！');
                }
                if($data['endtime']<time())
                {
                    jsont([], 1, '帐号过期');
                }
                if ($data['pwd'] != md5($_GPC['pwd'] . $data['salt'])) {
                    jsont([], 1, '手机号或密码错误！');
                }
                session('merchinfo', base64_encode(serialize($data)));
                jsont([],0,'登录成功');
            }catch (Exception $exception){
//                dump($exception->getMessage());die;
                jsont([], 1, $exception->getMessage());
            }

        }
        return $this->view->fetch();
    }

    public function out() {

        session('merchinfo', null);
        $url = $this->request->domain(true) . '/index/login';
        header('HTTP/1.1 303 See Other');
        header("Location: $url");
        exit;
    }

}
